from typing import List
import bisect

"""

dfs来搜索,超时
"""


class Solution:
    def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
        res = []
        l = []
        mem = []
        visited = [False] * len(people)

        def dfs():
            nonlocal l
            if len(res) == len(people):
                l = res[:]
                return
            for i in range(len(people)):
                if visited[i]:
                    continue
                if not res and people[i][1] != 0:
                    continue
                if len([x for x in res if x[0] >= people[i][0]]) == people[i][1]:
                    res.append(people[i])
                    visited[i] = True
                    dfs()
                    res.pop()
                    visited[i] = False

        dfs()
        return l


if __name__ == "__main__":
    # res = Solution().reconstructQueue([[6, 0], [5, 0], [4, 0], [3, 2], [2, 2], [1, 4]])
    res = Solution().reconstructQueue([[1, 0], [2, 0], [5, 0], [4, 1]])
    # res = Solution().reconstructQueue([[7, 0], [4, 4], [5, 0], [6, 1], [5, 2]])
    print("result=", res)
